<?php


namespace App\Utility;


use EasySwoole\EasySwoole\Config;

class McHelper
{

    public static function generateRandomFileName($random = '', $prefix = 'QE_FILE')
    {
        $hash = $prefix;
        $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        $new_chars = str_split($chars);
        shuffle($new_chars);
        $fn_str = '';
        $chars_key = array_rand($new_chars, 15);
        for ($i = 0; $i < 15; $i++) {
            $fn_str .= $new_chars[$chars_key[$i]];
        }
        $micro_time = time() . microtime(true);
        return $hash . '_' . $random . '_' . md5($fn_str . $micro_time);
    }

    public static function getFileExt($filename = '')
    {
        $result = pathinfo($filename);
        return $result['extension'];
    }


    public static function getFileUrl($data)
    {
        $config = Config::getInstance()->getConf('OSS');
        $storage = strtoupper($data['storage']);
        $cfg = $config[$storage]['BASE_URL'];
        return $cfg . '/' . $data['path'];

    }

}